package com.example.boottest.shiro.test;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.SimpleAccountRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Before;
import org.junit.Test;

/**
 * @Author: John
 * @Date 2019-10-24 21:40
 */
public class Realm {

    SimpleAccountRealm simpleAccountRealm = new SimpleAccountRealm();

    @Before
    public void init(){
        simpleAccountRealm.addAccount("admin","123456");
    }

    @Test
    public void testisAuthenticated(){
        //1.获取构建环境
        DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
        defaultSecurityManager.setRealm(simpleAccountRealm);
        //2.设置配置
        SecurityUtils.setSecurityManager(defaultSecurityManager);
        //3.获取登录用户
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken("admin","123456");
        subject.login(token);
        System.out.println("isAuthenticated()"+subject.isAuthenticated());

    }
}
